/*
 * @Author: tanying
 * @Date: 2024-04-15 13:58:08
 * @LastEditTime: 2024-04-16 16:30:42
 * @LastEditors: tanying
 * @FilePath: \vue-three\src\twin\CreateTwin.js
 * @Description: 
 */
import * as THREE from "three"
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js';
import { DRACOLoader } from 'three/examples/jsm/loaders/DRACOLoader.js';
import { RGBELoader } from 'three/examples/jsm/loaders/RGBELoader.js'
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';
import Helper from './Helper';

export default class CreateTwin {
  constructor(params = {}) {
    const { 
      logPosTargetBool = false,      
      helperBool = true, // 默认创建辅助开发工具
    } = params;

    // 场景
    this.scene = new THREE.Scene();
    // 相机
    const width = window.innerWidth; //窗口文档显示区的宽度作为画布宽度
    const height = window.innerHeight; //窗口文档显示区的高度作为画布
    this.camera = new THREE.PerspectiveCamera(30, width / height, 1, 3000);
    this.camera.position.set(200, 200, 200);
    this.camera.lookAt(0, 0, 0);
    // 渲染器
    this.renderer = new THREE.WebGLRenderer({
      antialias: true, // 渲染器锯齿属性
      logarithmicDepthBuffer: true,
    })
    this.renderer.setPixelRatio(window.devicePixelRatio);
    this.renderer.setSize(width, height); 
    document.body.appendChild(this.renderer.domElement);
    // gltf加载器
    // const draco = new DRACOLoader();
    // draco.setDecoderPath('./draco/');
    // this.loader = new GLTFLoader();
    // this.loader.setDRACOLoader(draco);
    this.loader = new GLTFLoader();
    // 循环渲染
    this.renderer.setAnimationLoop(() => {
      this.renderer.render(this.scene, this.camera)
    })
    // 平行光
    this.directionalLight = new THREE.DirectionalLight(0xffffff, 1);
    this.directionalLight.position.set(80, 100, 50);
    this.scene.add(this.directionalLight);
    // 设置.envMap
    const textureCube = new THREE.CubeTextureLoader()
      .setPath('./envMap/')
      .load(['px.jpg', 'nx.jpg', 'py.jpg', 'ny.jpg', 'pz.jpg', 'nz.jpg']);
      // CubeTexture表示立方体纹理对象

    this.scene.environment = textureCube;
    // const rgbeLoader = new RGBELoader();
    // rgbeLoader.load('./envMap.hdr', (envMap) => {
    //     this.scene.environment = envMap;
    //     envMap.mapping = THREE.EquirectangularReflectionMapping;
    // })
    // 相机控件
    this.controls = new OrbitControls(this.camera, this.renderer.domElement);
    this.controls.target.set(0, 0, 0);
    this.controls.update();

    // 画布尺寸随着窗口变化
    window.addEventListener('resize',()=>{
      this.renderer.setSize(window.innerWidth, window.innerHeight);
      this.camera.aspect = window.innerWidth / window.innerHeight;
      this.camera.updateProjectionMatrix();
    });
    
    
    if (logPosTargetBool) {
      this.controls.addEventListener('change',()=>{
        console.log('camera.position',this.camera.position);
        console.log('controls.target',this.controls.target);
      })
    }

    if (helperBool) {
      this.helper = new Helper(this.scene, this.directionalLight);
      this.gui = this.helper.gui;       
    }

    // 渲染循环
    this.renderer.setAnimationLoop(() => {
      if (helperBool) this.helper.stats.update();
      this.renderer.render(this.scene, this.camera);
    })

  }
}